/**
 * Created by admin on 2017/6/2.
 */

import {
  CityList,
  ActivityList,
  ActivityDetail,
  ActivityUserState,
  Register
} from '../mutation-types';
import { Cache, cache_keys } from '../../utils/util';
// 查询城市列表
export const queryCitylist =function({commit,state},page =1){
    EventBus.$http.get(EventBus.$Config.baseUrl+'api/city',{
      params:{
        page
      }
    }).then(res=>{
       if(res.data.status){
         let data=res.data.data

         if(page>1){
           data=state.cityList.concat(data);
         }

         commit(CityList,data);
         if(res.data.meta && res.data.meta.pagination){
           EventBus.$emit('page-loaded',true,res.data.meta.pagination);
         }else{
           EventBus.$emit('page-loaded',false);
         }
       }
       else{
          EventBus.$Modal.info({title:"消息提示",body:res.data.message || "加载错误"});
       }
    },()=>{
       EventBus.$Modal.info({title:"消息提示",body:'加载失败'});
    })
}

// 查询活动列表
export const queryActivityList =function({commit,state},data){
  let page=data.page || 1;
   EventBus.$http.get(EventBus.$Config.baseUrl+'api/activity/list/'+data.id,{
      params:{
         page
      }
   }).then(res => {
      if(res.data.status){
        if(page>1){
           res.data.data =state.activityList.data.concat(res.data.data);
        }
        commit(ActivityList,res.data);
        if(res.data.meta && res.data.meta.pagination){
          EventBus.$emit('page-loaded',true,res.data.meta.pagination);
        }
        else{
          EventBus.$emit('page-loaded',false);
        }
      }else{
         EventBus.$Modal.info({title:"消息提示",body:res.data.message || "加载错误"});
      }

   },()=>{
        EventBus.$Modal.info({title:"消息提示",body:"加载失败"});
   });
}

// 查询活动详情
export const queryActivityDeatil =function({commit,state},id){
  EventBus.$Indicator.blade();
  EventBus.$http.get(EventBus.$Config.baseUrl+'api/activity/show/'+id).then(res=>{
     if(res.data.status){
        let data=res.data.data;
        commit(ActivityDetail,data);
       EventBus.$Indicator.remove();
     }
     else{
        EventBus.$Modal.info({title:"消息提示",body:res.data.message || '加载错误'});
       EventBus.$Indicator.remove();
     }
  },()=>{
    EventBus.$Modal.info({title:"消息提示",body:"加载失败"});
    EventBus.$Indicator.remove();
  })
}

// 查询用户是否已喜欢、已报名活动
export const queryActivityUserState =function({commit,state},id){
    let oauth =Cache.get(cache_keys.token);
    EventBus.$http.get(EventBus.$Config.baseUrl+'api/activity/show/'+id+'/check',{
      headers: {
        Authorization: oauth.access_token
      }
    }).then(res=>{
      if(res.data.status){
         commit(ActivityUserState,res.data.data);
         EventBus.$emit('like-status',res.data.data.liked);
      }else{
         EventBus.$Modal.info({title:"消息提示",body:res.data.message || '加载错误'});
      }
    },()=>{
      EventBus.$Modal.info({title:"消息提示",body:"加载失败"});
    })
}


// 提交喜欢
export const submitlike =function({commit,state},id){
   let oauth =Cache.get(cache_keys.token);
   EventBus.$http.post(EventBus.$Config.baseUrl+'api/activity/like/' +id,{},{
       headers:{
         Authorization: oauth.access_token
       }
   }).then( res=>{
      if(res.data.status){
         EventBus.$emit('like-complete',res.data.data.liked);
      } else{
         EventBus.$Modal.info({title:"消息提示",body:res.message || "操作失败"});
      }
   },()=>{
       EventBus.$Modal.info({title:"消息提示",body:"加载失败"});
   })
}


//活动报名
export const queryRegister =function({commit,state},id){
   let oauth =Cache.get(cache_keys.token);
   EventBus.$http.get(EventBus.$Config.baseUrl+'api/activity/checkout/'+id,{
     headers: {
       Authorization: oauth.access_token
     }
   }).then(res=>{
      if(res.data.status){
         res.data.data.coupon.forEach((i,index)=>i.index =index);
         commit(Register,res.data.data);
      }
      else{
         EventBus.$Modal.info({title:"消息提示",body:res.data.message || '加载错误'});
      }
   },()=>{
     EventBus.$Modal.info({title:"消息提示",body:"加载失败"});
   });
}


// 提交报名
export const submitRegister =function({commit,state},data){
  let oauth =Cache.get(cache_keys.token);
  EventBus.$http.post(EventBus.$Config.baseUrl+'api/activity/checkout/'+data.id,{
    coupon_id:data.coupon_id
  },{
      headers:{
         Authorization:oauth.access_token
      }
    }).then(res=>{
     if(res.data.status){
        EventBus.$emit('register-complete',res.data);
     }
     else{
       EventBus.$Modal.info({title:"消息提示",body:res.data.message || '报名失败'});
     }
  },()=>{
    EventBus.$Modal.info({title:"消息提示",body: '加载失败'});
  });
}
